<?php

namespace App\Models;

use App\Common\Traits\HasAttachments;
use App\Common\Traits\HasAttributes;
use App\Common\Traits\HasValues;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class Commodity extends Base
{
    use HasAttributes, HasValues, HasAttachments, SoftDeletes, LogsActivity;

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function supplier()
    {
        return $this->hasOne(Supplier::class, 'id', 'supplier_id');
    }

    public function skus()
    {
        return $this->hasMany(Sku::class);
    }

    public function commodityActivities()
    {
        return $this->hasMany(CommodityActivity::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class, 'commodity_numbering', 'numbering');
    }

    public function afterSaleOrders()
    {
        return $this->hasMany(AfterSaleOrder::class);
    }

    /**
     * 自定义操作日志 说明
     * @param $eventName
     * @return string
     */
    public function getDescriptionForEvent(string $eventName): string
    {
        if (in_array($eventName, ['created', 'deleted', 'updated'])) {
            switch ($eventName) {
                case 'created':
                    return '创建商品:' . ($this['name'] ?? '');
                case 'deleted':
                    return sprintf('删除商品: %s (%s)', $this['name'] ?? '', $this['numbering'] ?? '');
                case 'updated':
                    return '编辑商品:' . ($this['name'] ?? '') . ' (' . ($this['numbering'] ?? '') . ')';
            }
        }

        return '';
    }
}
